<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/6/22 15:27
 */

namespace App\Project\Filters;

class DisposeFilter extends FilterAbstract
{
    protected function forExtend()
    {
        $map = [
            'control_objectives.hba1c'               => 1,
            'control_objectives.fbg'                 => 1,
            'control_objectives.2hpbg'               => 1,
            'control_objectives.blood_pressure'      => 1,
            'control_objectives.blood_pressure.low'  => 1,
            'control_objectives.blood_pressure.high' => 1,
            'control_objectives.ldl-c'               => 1,
            'control_objectives.TG'                  => 1,
            'control_objectives.TC'                  => 1,

            'advise.matters.accepted'             => 1,
            'advise.diet.accepted'                => 1,
            'advise.diet.item'                    => 1,
            'advise.diet.staple'                  => 1,
            'advise.diet.breakfast'               => 1,
            'advise.diet.lunch'                   => 1,
            'advise.diet.dinner'                  => 1,
            'advise.diet.meat'                    => 1,
            'advise.low_salt.accepted'            => 1,
            'advise.low_salt.item'                => 1,
            'advise.regular_exercise.accepted'    => 1,
            'advise.regular_exercise.item'        => 1,
            'advise.rest_schedule.accepted'       => 1,
            'advise.rest_schedule.item'           => 1,
            'advise.smbg.accepted'                => 1,
            'advise.smbg.items'                   => 1,
            'advise.smbg.items.before_breakfast'  => 1,
            'advise.smbg.items.after_breakfast_2' => 1,
            'advise.smbg.items.before_lunch'      => 1,
            'advise.smbg.items.after_lunch_2'     => 1,
            'advise.smbg.items.before_dinner'     => 1,
            'advise.smbg.items.after_dinner_2'    => 1,
            'advise.smbg.items.before_sleep'      => 1,
            'advise.smbg.items.morning'           => 1,

            'advise.change_treatment_plan.accepted' => 1,

            'advise.lose_weight.accepted' => 1,
            'advise.lose_weight.item'     => 1,

            'advise.take_drugs.accepted'       => 1,
            'advise.follow_up_clinic.accepted' => 1,
            'advise.low_protein.accepted'      => 1,
            'advise.low_fat.accepted'          => 1,
            'advise.low_purine.accepted'       => 1,

            'advise.pec.accepted' => 1,
            'advise.pec.other'    => 1,

            'advise.other.accepted' => 1,
        ];

        for ($i = 0; $i < 11; $i++) {
            $map['advise.pec.items.' . $i] = 1;
        }

        $map['inspection_plan'] = $this->inspectionPlan();
        $map['schedule']        = $this->schedule();
        $map['medication']      = $this->medication($this->request->input('medication'));
        $map['cn_medication']   = $this->cnMedication();

        return $map;
    }

    protected function inspectionPlan()
    {
        $map = [];

        $plan = $this->request->input('inspection_plan');
        if (empty($plan)) {
            return 1;
        }

        $keys = [
            'blood_fat',
            'body_sensations',
            'kidney',
            'liver',
            'blood_routine',
            'hbalc',
            'ga',
            'urine_routine',
            'uacr',
            'thyroid1',
            'thyroid2',
            'thyroid3',
            'fundus',
            'abi/tbi',
            'leau',
            'cau',
            'uu',
            'mlecg',
            'au',
            'hemoglobin',
            'diabetes_antibodies1',
            'ogtt',
            'c-p',
            'ins'
        ];

        foreach ($keys as $key) {
            $map[$key] = ['accepted' => 1, 'cycle' => 1];
        }

        $count = count((array)$this->request->input('inspection_plan.extend'));

        if ($count > 0) {

            for ($i = 0; $i < $count; $i++) {
                $map['extend'][$i] = ['title' => 1, 'cycle' => 1];
            }
        }

        return $map;
    }

    protected function medication($medication)
    {
        if (empty($medication)) {
            return 1;
        }
        $map = [];

        if (is_array($medication)) {
            foreach ($medication as $key => $item) {
                $map[$key] = array_flip([
                    'name', 'dosage.type', 'dosage.value', 'dosage.values.morning',
                    'dosage.unit', 'start_time', 'end_time', 'usage',
                    'frequency', 'remark', 'dosage.values.atnoon', 'dosage.values.evening',
                    'dosage.values.before_sleep'
                ]);
            }
        }

        return $map;
    }

    protected function schedule()
    {
        return [
            'transfer_treatment' => 1,
            'plan'               => 1,
            'date'               => 1,
        ];
    }

    protected function cnMedication()
    {
        $map = [];

        $medication = $this->request->input('cn_medication');

        if (is_array($medication) && ($count = count($medication)) > 0) {

            $keys = [];

            for ($i = 0; $i < $count; $i++) {

                if ($medication[$i]['type'] == 1) {
                    $drugs = count($medication[$i]['drugs']);

                    for ($j = 0; $j < $drugs; $j++) {
                        $keys[$j] = array_flip(['name', 'weight']);
                    }

                    $map[$i] = array_merge(array_flip(['usage', 'dosage', 'frequency', 'dose', 'type']),
                        ['drugs' => $keys]);
                } elseif ($medication[$i]['type'] == 3) {
                    $map[$i] = array_flip([
                        'name', 'start_time', 'end_time', 'times_per_day', 'quantity_per_times',
                        'quantity_unit', 'total', 'type'
                    ]);
                }
            }
        }

        return $map;
    }

    protected function getRootFieldsHasNoSon()
    {
        return [
            'id', 'record_id', 'control_objectives', 'advise', 'inspection_plan', 'schedule', 'medication',
            'cn_medication', 'signatures', 'remark'
        ];
    }

    protected function getRootFieldsHasSon()
    {
        return [];
    }

    protected function getSonFieldsForEachRoot()
    {
        return [];
    }
}